//
//  testApp.swift
//  test
//
//  Created by 下坠 on 2025/3/25.
//

import SwiftUI

@main
struct testApp: App {
    @StateObject private var themeManager = ThemeManager()
    @StateObject private var languageManager = LanguageManager()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(themeManager)
                .environmentObject(languageManager)
                .preferredColorScheme(themeManager.currentScheme)
                .dynamicTypeSize(.xSmall ... .accessibility2)
        }
    }
}

class ThemeManager: ObservableObject {
    @Published var selectedTheme: String {
        didSet {
            UserDefaults.standard.set(selectedTheme, forKey: "selectedTheme")
        }
    }
    
    var currentScheme: ColorScheme? {
        switch selectedTheme {
        case "light": return .light
        case "dark": return .dark
        default: return nil // 跟随系统
        }
    }
    
    init() {
        self.selectedTheme = UserDefaults.standard.string(forKey: "selectedTheme") ?? "system"
    }
}

// 从ContentView.swift移动LanguageManager定义到这里
class LanguageManager: ObservableObject {
    @Published var currentLanguage: Language {
        didSet {
            UserDefaults.standard.set(currentLanguage.rawValue, forKey: "selectedLanguage")
            NotificationCenter.default.post(name: NSNotification.Name("LanguageChanged"), object: nil)
        }
    }
    
    init() {
        let savedLanguage = UserDefaults.standard.string(forKey: "selectedLanguage") ?? "zh-Hans"
        self.currentLanguage = Language(rawValue: savedLanguage) ?? .chinese
    }
    
    func setLanguage(_ language: Language) {
        currentLanguage = language
    }
}

enum Language: String, CaseIterable {
    case chinese = "zh-Hans"
    case english = "en"
    case uyghur = "ug"
    
    var displayName: String {
        switch self {
        case .chinese: return "中文"
        case .english: return "English"
        case .uyghur: return "ئۇيغۇرچە"
        }
    }
    
    var flag: String {
        switch self {
        case .chinese: return "🇨🇳"
        case .english: return "🇺🇸"
        case .uyghur: return "🇨🇳"
        }
    }
}
